package com.qf.j2112.controller;

import com.alibaba.fastjson.JSON;
import com.qf.j2112.anno.JWTAnno;
import com.qf.j2112.po.Cart;
import com.qf.j2112.po.TourArea;
import com.qf.j2112.service.CartService;
import com.qf.j2112.service.TourAreaService;
import com.qf.j2112.utils.CartUtil;
import com.qf.j2112.utils.JwtUtil;
import com.qf.j2112.vo.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class CartController {

    @Resource
    private CartService cartService;

    @Resource
    private TourAreaService tourAreaService;

    //用户添加一个商品到购物车
    @JWTAnno(name = "用户添加商品到购物车")
    @PostMapping("/addToCart")
    public ActionResultVO addToCart(@RequestBody CartVO cartVO ){
        ActionResultVO vo=null;
        LinkedHashMap<String,Object> goods = (LinkedHashMap) cartVO.getGoods();
        ClientCartVo clientCartVo = JSON.parseObject(JSON.toJSONString(goods), ClientCartVo.class);
        System.out.println(cartVO.getNum());
        clientCartVo.setCartCount(cartVO.getNum());
        clientCartVo.setCreateTime(new Date());
        clientCartVo.setUid(Integer.parseInt(JwtUtil.getUsrInfoFromToken(cartVO.getToken())));
        boolean b = cartService.addToCart(clientCartVo);
        if (b){
            vo=new ActionResultVO(200,"添加购物车成功",null);
        }else {
//            System.out.println("添加购物车失败");
            vo=new ActionResultVO(-200,"添加购物车失败",null);
        }
        return vo;
    }

    //修改购物车中商品的数量
    @JWTAnno(name = "用户修改购物车中一个商品的数量")
    @PostMapping("/updateCartCount")
    public  ActionResultVO updateCartNum(@RequestBody CartVO cartVO ){
        ActionResultVO vo=null;
        if (cartVO.getNum()>=1){
            Cart cart=new Cart();
            cart.setCartId(cartVO.getCartId());
            cart.setNum(cartVO.getNum());
            if(cartService.updateOnesCart(cart)){
                vo=new ActionResultVO(200,"修改商品数量成功",null);
            }else{
                vo=new ActionResultVO(-200,"修改商品数量失败",null);
            }
        }else {
            vo=new ActionResultVO(-200,"修改商品数量失败，数量不能小于1",null);
        }
        return vo;
    }

    //删除购物车一件商品,根据id
    @JWTAnno(name = "用户删除购物车商品")
    @PostMapping("/delFromCart")
    public ActionResultVO deleteCart(@RequestBody CartVO cartVO){
        ActionResultVO vo=null;
//        System.out.println(cartVO.getToken());
//        String usrInfoFromToken = JwtUtil.getUsrInfoFromToken(cartVO.getToken());
        List<Integer> delId = cartVO.getDelId();
        boolean b=true;
        for (Integer integer : delId) {
            b =cartService.deleteCart(integer);
        }
        if(b){
            vo=new ActionResultVO(200,"删除商品成功",null);
        }else{
            vo=new ActionResultVO(-200,"删除商品失败",null);
        }
        return vo;
    }

    //用户清空购车(未用到)
    @JWTAnno(name = "用户清空购车")
    @DeleteMapping("/clearCart")
    public ActionResultVO clearCart(int uid){
        ActionResultVO vo=null;
        if(cartService.clearOnesCart(uid)){
            vo=new ActionResultVO(200,"删除商品成功",null);
        }else{
            vo=new ActionResultVO(-200,"删除商品失败",null);
        }
        return vo;
    }

    //获取一个用户的购物车信息
    @JWTAnno(name = "用户获取购物车信息")
    @PostMapping ("/getCart")
    public ClientActionResultVo getOnesCart(@RequestBody CartVO cartVO){
        ClientActionResultVo vo=null;
        String usrInfoFromToken = JwtUtil.getUsrInfoFromToken(cartVO.getToken());
        int uid = Integer.parseInt(usrInfoFromToken);
        List<Cart> cartList = cartService.getOnesCart(uid);
        if(!ObjectUtils.isEmpty(cartList)){
            List<ClientCartVo> cartVos = new ArrayList<>();
            for (Cart cart : cartList) {
                ClientCartVo clientCartVo = CartUtil.cartVo(cart);
                cartVos.add(clientCartVo);
            }
            vo=new ClientActionResultVo(200,"获取购物车成功",cartVos);
        }else{
            vo=new ClientActionResultVo(200,"购物车为空",null);
        }
        return vo;
    }

}
